package com.zzw.bridge.business.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 类描述
 *
 * @author XiaoJie
 * @date 2025/5/19 16:20
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {
    @Bean(name = "fileTaskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置线程池大小为CPU核心数的2倍，平衡I/O密集型任务
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 4);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("async-file-");
        executor.initialize();
        return executor;
    }
}
